const service = require('../../../service/port.js')
const rules = require('../../../utils/util.js')
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    radio: '1',
    storeList: [],
    receiver:'',
    receiverMobile:'',
    currentStore:'',
    currentStoreName:'',
    phoneErrorText:'',
    nameErrorText:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    service.getStore({},(res) => {
      this.setData({
        storeList: res.data,
      })
    })
    service.getStoreList(res => {
      console.log(res);

      
    })
  },

  choose: function() {
    this.setData({
      show: true
    });
  },
  onClose: function() {
    this.setData({
      show: false
    });
  },
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  blurEvent1(e) {
    if (e.detail.value == '') {
      this.setData({
        phoneErrorText: '手机号不能为空！',
      });
    } else {
      this.setData({
        phoneErrorText: '',
        receiverMobile: e.detail.value
      });
    }
  },
  blurEvent(e) {

    if (e.detail.value == '') {
      this.setData({
        nameErrorText: '姓名不能为空！',
      });
    } else {
      this.setData({
        nameErrorText: '',
        receiver: e.detail.value
      });
    }
  },
  onChangephone(e) {
    console.log(e.detail)
    if (!rules.regPhone(e.detail)) {
      this.setData({
        phoneErrorText: '手机号格式有误！',
      });
    } else {
      this.setData({
        phoneErrorText: '',
      });
    }
  },
  onClick(event) {
    console.log(event.currentTarget);
    const {
      name
    } = event.currentTarget.dataset;
    this.setData({
      radio: name,
      currentStore: event.currentTarget.dataset.item,
      currentStoreName: event.currentTarget.dataset.item.storeName
    });
  },
  goOrder(){
    if (this.data.currentStore==''){
      Toast.fail('请选择门店');
      return
    }
    if (this.data.receiver == '') {
      this.setData({
        nameErrorText: '姓名不能为空！',
      });

      return
    };
    if (this.data.receiverMobile == '') {
      this.setData({
        phoneErrorText: '收件人手机号不能为空！',
      });
      return
    };
    if (!rules.regPhone(this.data.receiverMobile)) {
      this.setData({
        phoneErrorText: '收件人手机号格式有误！',
      });
      return
    }
    var store = this.data.currentStore;
    store.receiver = this.data.receiver;
    store.receiverMobile = this.data.receiverMobile
    wx.setStorageSync('currentStore', store)
    // wx.navigateTo({
    //   url: '/pages/order/confirmorder/index?store=' + JSON.stringify(store),
    // })
    wx.navigateBack({
      delta: 1
    })
  }
})